<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Customer_health_model extends CI_Model
{
	private function get_last_push_info_id($uid)
	{
		$query = $this->db->query("select max(noticeId) as maxNoticeId from userhealthinfopushnotice where uid=$uid;");
		if(!$query->num_rows())
			return null;
		
		$row = $query->row_array();
		
		return $row["maxNoticeId"];
			
	}
	
	public function get_last_health_info_id($uid)
	{
		$query = $this->db->query("select max(healthInfoId) as maxHealthInfoId from userhealthinfo where uid=$uid;");
		if(!$query->num_rows())
			return null;
		$row = $query->row_array();
		
		return $row["maxHealthInfoId"];
	}
	
	public function write_new_health_data($field, $data)
	{
		$record = get_recent_health_info(get_uid());
	
		$record["date"] = mysql_datetime();
		$record["uid"] = get_uid();
		$record[$field] = $data;
		unset($record["healthInfoId"]);
	
		$this->db->set($record);
		$this->db->insert("userhealthinfo");
	
		return  $this->db->insert_id();
	}
	
	public function update_health_data($uid, $field, $data)
	{
	
		if(!$this->should_write_new_health_data($uid, $field, $data))
			return;
	
		$health_info_id = $this->write_new_health_data($field, $data);
		$this->push_health_info($uid, $field, $health_info_id);
	}
	
	public function should_push_health_info($uid, $field, $data)
	{
		if(!$field)
			return false;
		
		$noticeId = $this->get_last_push_info_id($uid);
		if($noticeId === null)
			return true;
		
		$this->db->where("noticeId", $noticeId);
		$query = $this->db->get("userhealthinfopushnotice");
		$row = $query->row_array();
		
		if($row["column"] != $field)
			return true;
		
		$health_info_id = $row["infoId"];
		$max_health_info_id = $this->get_last_health_info_id($uid);
		
		$this->db->where("healthInfoId", $health_info_id);
		$query = $this->db->get("userhealthinfo");
		$row1 = $query->row_array();
		
		$this->db->where("healthInfoId", $max_health_info_id);
		$query = $this->db->get("userhealthinfo");
		$row2 = $query->row_array();
		
		if($row1[$field] == $row2[$field])
			return false;
		
		return true;
	}
	
	public function should_write_new_health_data($uid, $field, $data)
	{
		if(!$field)
			return;
		
		$last_health_info_id = $this->get_last_health_info_id($uid);
		if($last_health_info_id === null)
			return true;
		
		$this->db->where("healthInfoId", $last_health_info_id);
		$query = $this->db->get("userhealthinfo");
		
		$row = $query->row_array();
		
		if($row[$field] == $data)
			return false;
		
		return true;
	}
	
	public function get_all_shared_health_info($uid)
	{
		$last_health_info_id = $this->get_last_health_info_id($uid);
		
		$this->db->where("healthInfoId",$last_health_info_id);
		$query = $this->db->get("userhealthinfo");
		
		if(!$query->num_rows())
			return null;
		
		$last_health_info = $query->row_array();
		
		$shared_health_info = array();
		foreach($last_health_info as $field=>$value)
		{
			if(!field_shared($uid, $field))
				continue;
			
			$shared_health_info[] = array(
					"field"=>$field,
					"health_info_id"=>$last_health_info["healthInfoId"]
					);
		}
		
		return $shared_health_info;
	}
	
	public function push_health_info($uid, $field, $health_info_id)
	{
		$this->db->where("uid", $uid);
		$query = $this->db->get("userfriends");
	
		if(!$query->num_rows())
			return;
	
		if(!field_shared($uid, $field))
			return;
	
		$friend_uid_ary = array();
		$friend_rows = $query->result();
		foreach($friend_rows as $row)
		{
			$friend_uid_ary[] = $row->friendUid;
		}
	
		foreach($friend_uid_ary as $friend_uid)
		{
			$this->push_health_info_to_friend($uid, $friend_uid, $field, $health_info_id);
		}
	}
	
	public function push_health_info_to_friend($uid, $friend_uid, $field, $health_info_id)
	{
		$record["uid"] = $uid;
		$record["friendUid"] = $friend_uid;
		$record["column"] = $field;
		$record["date"] = mysql_datetime();
		$record["infoId"] = $health_info_id;
		$this->db->insert("userhealthinfopushnotice", $record);
	}
	
	public function is_health_info_shared_by($uid, $field_id)
	{
		$this->db->where("uid", $uid);
		$query = $this->db->get("usershare");
		$row = $query->row();
		
		return $row->$field_id;
	}
	
}
?>
